﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid;
using BusinessObjects;
using DataAccess;

namespace QuanLyHopDong
{
    public partial class BacLuongListView : Form
    {
        private BindingList<BacLuong> bindingListBacLuong;

        private BacLuong _bacLuong;

        public BacLuongListView()
        {
            InitializeComponent();
        }

        private void BacLuongListView_Load(object sender, EventArgs e)
        {
            string[] listFieldName = new string[] { "MaQuanLy", "TenBacLuong", "HeSoLuong", "NgachLuong.TenNgachLuong" };
            string[] listColumnName = new string[] { "Mã Quản Lý", "Tên Bậc Lương", "Hệ Số Lương", "Ngạch Lương" };
            AppGridView.InitGridView(gvBacLuong, false, false, DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect, false, false, false, true);
            AppGridView.ReadOnlyGridView(gvBacLuong);
            AppGridView.ShowField(gvBacLuong, listFieldName, listColumnName);

            LoadData();
        }

        private void btnThem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            _bacLuong = new BacLuong();
            ShowDetailForm();
        }

        private void btnXoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DeleteRow();
        }

        private void btnDong_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void btnLamMoi_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LoadData();
        }

        private void gcBacLuong_DoubleClick(object sender, EventArgs e)
        {
            _bacLuong = gvBacLuong.GetFocusedRow() as BacLuong;
            ShowDetailForm();
        }
        void LoadData()
        {
            bindingListBacLuong = new BindingList<BacLuong>(DataAccessObject.BacLuong.GetListBacLuong());

            bindingSourceBacLuong.DataSource = bindingListBacLuong;
            gvBacLuong.Invalidate();
        }

        void ShowDetailForm()
        {
            BacLuongDetailView detailView = new BacLuongDetailView(_bacLuong);
            detailView.SaveChanged += new EventHandler(detailView_SaveChanged);
            detailView.DeleteChanged += new EventHandler(detailView_DeleteChanged);
            //detailView.MdiParent = this.MdiParent;
            detailView.Show();
        }

        void DeleteRow()
        {
            if (MessageBox.Show("Bạn có chắc xóa?", "Xóa một dòng thông tin học kỳ", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                _bacLuong = gvBacLuong.GetFocusedRow() as BacLuong;

                if (_bacLuong != null && _bacLuong.Oid > 0)
                
                    if (DataAccessObject.BacLuong.Delete(_bacLuong) > 0)
                    {
                        gvBacLuong.DeleteSelectedRows();
                        gvBacLuong.Invalidate();
                    }
                    else
                        MessageBox.Show("Không xóa được", "Có Lỗi Xảy Ra", MessageBoxButtons.OK, MessageBoxIcon.Error);
                
            }
        }

        void detailView_DeleteChanged(object sender, EventArgs e)
        {
            DeleteRow();
        }

        void detailView_SaveChanged(object sender, EventArgs e)
        {
            bindingListBacLuong.Add(_bacLuong);
            gvBacLuong.Invalidate();
        }
    }
}
